package com.jzx.jt808.utils;

/**
 * 类描述：BCD工具类<br/>
 * 由于hu-tool要在6.0废弃BCD码工具类<br/>
 *
 * @author yangjie
 * @date 2023-07-13 16:09
 **/
public class BcdUtils {
    /**
     * BCD字节数组转String
     *
     * @param bytes
     * @return 十进制字符串
     */
    public static String bcdToStr(byte[] bytes) {
        StringBuilder temp = new StringBuilder(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            // 高四位
            temp.append((bytes[i] & 0xf0) >>> 4);
            // 低四位
            temp.append(bytes[i] & 0x0f);
        }
        return temp.toString();
    }

    /**
     * 字符串转BCD字节数组
     *
     * @param str
     * @return BCD字节数组
     */
    public static byte[] strToBcd(String str) {
        // 奇数,前补零
        if ((str.length() & 0x1) == 1) {
            str = "0" + str;
        }

        byte ret[] = new byte[str.length() / 2];
        byte bs[] = str.getBytes();
        for (int i = 0; i < ret.length; i++) {

            byte high = ascII2Bcd(bs[2 * i]);
            byte low = ascII2Bcd(bs[2 * i + 1]);
            // 遮罩BCD低四位
            ret[i] = (byte)((high << 4) | low);
        }
        return ret;
    }

    private static byte ascII2Bcd(byte asc) {
        if ((asc >= '0') && (asc <= '9'))
            return (byte)(asc - '0');
        else if ((asc >= 'A') && (asc <= 'F'))
            return (byte)(asc - 'A' + 10);
        else if ((asc >= 'a') && (asc <= 'f'))
            return (byte)(asc - 'a' + 10);
        else
            return (byte)(asc - 48);
    }
}
